package com.xnfh.framework.base

import com.xnfh.framework.Constants
import com.xnfh.framework.R
import com.xnfh.framework.bean.BaseResp
import com.xnfh.framework.bean.RespStateData
import com.xnfh.framework.log.LogUtil
import com.xnfh.framework.toast.TipsToast
import retrofit2.HttpException
import java.net.ConnectException
import java.net.UnknownHostException

/**
 * @Description:
 * @author ziheng
 * @date 2022/12/5 17:29
 */
open class BaseRepository {

    private val TAG = "BaseRepository"

    suspend fun <T> dealResp(
        block: suspend () -> BaseResp<T>,
        liveData: RespStateData<T>,
    ) {

        var result = BaseResp<T>()
        result.responseState = BaseResp.ResponseState.REQUEST_START
        liveData.postValue(result)
//        if (!NetWorkUtil.isNetworkConnected(ActManager.Instance().fristActivity)) {
//            result.responseState = BaseResp.ResponseState.REQUEST_FAILED
//            return
//        }

        try {

            //---------------------//
            result = block.invoke()
            //---------------------//
            LogUtil.d("$result", tag = TAG)
            val code = if(result.statusCode != -1){
                result.statusCode
            }else if(result.retCode != null){
                result.retCode
            }else if(result.smsCode != -1){
                result.smsCode
            }else{
                result.status
            }
            when (code) {
                Constants.HTTP_SUCCESS -> {
                    result.responseState = BaseResp.ResponseState.REQUEST_SUCCESS
                }
                //天气相关的，后面code统一之后可以删掉
                Constants.HTTP_SUCCESS_WEATHER ->{
                    result.responseState = BaseResp.ResponseState.REQUEST_SUCCESS
                }
                //登录验证码相关的
                Constants.HTTP_SMS_SUCCESS ->{
                    result.responseState = BaseResp.ResponseState.REQUEST_SUCCESS
                }
                Constants.HTTP_AUTH_INVALID -> {
                    result.responseState = BaseResp.ResponseState.REQUEST_LOGOUT
                    TipsToast.showWarningTips(R.string.common_logout)
//                    UserInfoManager.instance.clearUserInfo()
//                    val eventMsg = EventMessageBean::class.java.newInstance()
//                    eventMsg.tag = EventBusTag.CLEAR_TOEKN_TAG
//                    EventBus.getDefault().post(eventMsg)
                }
                Constants.HTTP_ERROR ->{
                    result.responseState = BaseResp.ResponseState.REQUEST_ERROR
                    TipsToast.showTips(result.retMsg)
                }
                else -> {
                    result.responseState = BaseResp.ResponseState.REQUEST_FAILED
                    TipsToast.showWarningTips(result.message)
                }
            }

        } catch (e: Exception) {
            LogUtil.e(e.message.toString(),e,TAG)
            when (e) {
                is UnknownHostException,
                is HttpException,
                is ConnectException,
                -> {
                    //网络error
                    TipsToast.showWarningTips(R.string.common_net_error)
                }
                else -> {
                    LogUtil.e(e.message.toString(), tag = TAG)
                }
            }
            result.responseState = BaseResp.ResponseState.REQUEST_ERROR
        } finally {
            liveData.postValue(result)
        }
    }

}